#include "ULUI_LookupRange.h"
#include "UL_Global.h"
#include "ULGfx_History.h"

#define RANGE_LOOKUP_START_X	100
#define RANGE_LOOKUP_START_Y	100

ULUI_LookupRange ULUI_LookupRange::rangeLookup;

enum HistoryLookupIDs{
	SERIAL_NUMBER_BOX,
	DATE_BOX,
	RANGE_BOX,
	OK,
	EXIT	
};	

static char *menuTitles[] = {
	"                                ",
	"                                ",
	"                                ",
	"确定",
	"返回",
};

#define FIXED_GAP	20
#define SERIALS_PER_SCREEN	8

void ULUI_LookupRange::Init()
{
	int i;
	ULUI_Menu menu;

	AttachEventHandler(this);

	pad.Reset();
	pad.EnableSolidBg(true);
	menu.SetFont(UL_Global::UnionLottoFont());
	pad.SetFont(UL_Global::UnionLottoFont());
	selectedMenu = -1;

	for (i = 0; i <= EXIT; i++){
		menu.SetID(i);
		menu.SetTitle(menuTitles[i]);
		menus.AddMenu(menu);
	}

	//Find menu's X is after tip
	menu.SetTitle("                                ");
	menu.GetTitleSize(tipWidth, tipHeight);
	menus.SetFirstMenuXY(VSDL_Point(RANGE_LOOKUP_START_X + tipWidth, RANGE_LOOKUP_START_Y + tipHeight));
	pad.SetStartXY(VSDL_Point(RANGE_LOOKUP_START_X + tipWidth * 2 + FIXED_GAP, RANGE_LOOKUP_START_Y));
	selectedBox = SERIAL_NUMBER_BOX;
	startSerialNo = 0;
	serialOffset = 0;
	nSerials = 0;
}

void ULUI_LookupRange::CleanUp()
{
	pad.Reset();
	menus.Reset();
}

void ULUI_LookupRange::Pause()
{
}

void ULUI_LookupRange::Resume()
{
	selectedMenu = -1;
}

void ULUI_LookupRange::Update(VSDL_GameEngine *game)
{
	char *str;
	ULG_History *ulgHistory = ULG_History::Instance();

	if (selectedMenu == -1){
		return ;
	}

	str = pad.GetString();

	if (str != NULL){
		switch (selectedBox){
			case SERIAL_NUMBER_BOX:
				serialBoxString = str;
				startSerialNo = strtoul(str, NULL, 10);
				break;
			case DATE_BOX:
				ULC_Serial *serial;
				dateBoxString = str;
				//Lookup
				serial = ulgHistory->LookupSerial(str); 
				if (serial != NULL){
					startSerialNo = serial->sn;
				}else{
					startSerialNo = 0;
				}
				break;
			case RANGE_BOX:
				rangeBoxString = str;
				nSerials = strtoul(str, NULL, 10); 
				break;
		}
	}

	switch (selectedMenu){
		case SERIAL_NUMBER_BOX:
			if (selectedBox != SERIAL_NUMBER_BOX){
				selectedBox = SERIAL_NUMBER_BOX;
				dateBoxString.clear();
				pad.ClearString();
			}
			break;
		case DATE_BOX:
			if (selectedBox != DATE_BOX){
				selectedBox = DATE_BOX;
				serialBoxString.clear();
				pad.ClearString();
			}
			break;
		case RANGE_BOX:
			if (selectedBox != RANGE_BOX){
				selectedBox = RANGE_BOX;
				pad.ClearString();
			}
			break;
		case OK:
			if (startSerialNo <= 0){
				readyToGo = false;
				break;
			}

			if (rangeBoxString.length() != 0){
				readyToGo = true;
			}else{
				readyToGo = false;
			}

			break;
		case EXIT:
			game->PopState();
			break;
	}
}


void ULUI_LookupRange::Draw(VSDL_GameEngine *game)
{
	char *str;
	VSDL_RGB rgbNormal(100, 100, 100);
	VSDL_RGB rgbSelected(250, 100, 100);
	VSDL_RGB rgb;
	VSDL_Point currentXY(RANGE_LOOKUP_START_X, RANGE_LOOKUP_START_Y);
	SDL_Surface *destSurf = game->GetSurface();
	ULG_History *ulgHistory = ULG_History::Instance();;
	int serialNo;
	int historyY = RANGE_LOOKUP_START_Y + (EXIT + 2) * tipHeight;


	ulgHistory->SetStartXY(VSDL_Point(RANGE_LOOKUP_START_X, historyY));

	UL_Global::UnionLottoShowBg();
	UL_Global::UnionLottoShowTitle();
	menus.Show(destSurf);
	//Draw history data background
	SPG_RectFilled(destSurf, RANGE_LOOKUP_START_X, historyY, destSurf->w, destSurf->h, rgbNormal.GetRGB32(destSurf));
	//Show tips
	UL_Global::UnionLottoShowTip("输入任意一项进行查询,翻页使用PAGEUP和PAGEDOWN", currentXY);
	currentXY.y += tipHeight;
	UL_Global::UnionLottoShowTip("起始期号:(例:2012001)", currentXY);

	str = pad.GetString();

	if (selectedBox == SERIAL_NUMBER_BOX){
		rgb = rgbSelected;
		
		if (str != NULL){
			UL_Global::UnionLottoShowTip(str, VSDL_Point(currentXY.x + tipWidth, currentXY.y));
		}

	}else{
		rgb = rgbNormal;
	}

	if (serialBoxString.length() > 0){
		UL_Global::UnionLottoShowTip(serialBoxString.c_str(), VSDL_Point(currentXY.x + tipWidth, currentXY.y));
	}

	SPG_Rect(destSurf, currentXY.x + tipWidth, currentXY.y, currentXY.x + tipWidth * 2, currentXY.y + tipHeight, rgb.GetRGB32(destSurf));
	rgb = rgbNormal;

	currentXY.y += tipHeight;
	UL_Global::UnionLottoShowTip("起始日期:(例:2012-01-01)", currentXY);

	if (selectedBox == DATE_BOX){
		rgb = rgbSelected;

		if (str != NULL){
			UL_Global::UnionLottoShowTip(str, VSDL_Point(currentXY.x + tipWidth, currentXY.y));
		}

	}else{
		rgb = rgbNormal;
	}

	if (dateBoxString.length() > 0){
		UL_Global::UnionLottoShowTip(dateBoxString.c_str(), VSDL_Point(currentXY.x + tipWidth, currentXY.y));
	}

	SPG_Rect(destSurf, currentXY.x + tipWidth, currentXY.y, currentXY.x + tipWidth * 2, currentXY.y + tipHeight, rgb.GetRGB32(destSurf));
	rgb = rgbNormal;


	currentXY.y += tipHeight;
	UL_Global::UnionLottoShowTip("期数:(例:16)", currentXY);


	if (selectedBox == RANGE_BOX){
		rgb = rgbSelected;

		if (str != NULL){
			rangeBoxString = str;
		}
	}else{
		rgb = rgbNormal;
	}

	if (rangeBoxString.length() > 0){
		UL_Global::UnionLottoShowTip(rangeBoxString.c_str(), VSDL_Point(currentXY.x + tipWidth, currentXY.y));
	}

	SPG_Rect(destSurf, currentXY.x + tipWidth, currentXY.y, currentXY.x + tipWidth * 2, currentXY.y + tipHeight, rgb.GetRGB32(destSurf));

	//Draw input pad
	pad.Show(destSurf);
	//Draw spliting line
	historyY--;
	SPG_Line(destSurf, currentXY.x, historyY, destSurf->w, historyY, rgbNormal.GetRGB32(destSurf));
	//Show range
	if (readyToGo){
		if (nSerials <= SERIALS_PER_SCREEN){
			ulgHistory->ShowRange(destSurf, startSerialNo, nSerials);
		}else{
			ulgHistory->ShowRange(destSurf, startSerialNo + serialOffset, SERIALS_PER_SCREEN);		
		}
	}
}

ULUI_LookupRange *ULUI_LookupRange::Instance()
{
	return &rangeLookup;
}


void __HANDLE_EVENT ULUI_LookupRange::OnKeyDown(SDLKey keysym, unsigned char *keyStates, void *arg)
{
	VSDL_GameEngine *game = reinterpret_cast<VSDL_GameEngine *>(arg);

	//Basic
	if (keyStates[SDLK_ESCAPE]){
		game->PopState();
	}

	//Special cases
	if (keyStates[SDLK_RETURN]){
		selectedMenu = OK;
		return ;
	}

	if (keyStates[SDLK_UP]){
		selectedMenu--;

		if (selectedMenu < 0){
			selectedMenu = 0;
		}

		return ;
	}

	if (keyStates[SDLK_DOWN]){
		selectedMenu++;

		if (selectedMenu > OK){
			selectedMenu = OK;
		}

		return ;
	}

	if (keyStates[SDLK_PAGEUP]){
		if (serialOffset > 0){
			serialOffset--;
		}
	}

	if (keyStates[SDLK_PAGEDOWN]){
		serialOffset++;

		if (serialOffset + SERIALS_PER_SCREEN >= nSerials){
			serialOffset = nSerials - SERIALS_PER_SCREEN;
		}
	}

	pad.PushKeysym(keysym);
}

void __HANDLE_EVENT ULUI_LookupRange::OnMouseMove(int button, unsigned char buttonState, int absX, int absY, int relX, int relY, void *arg)
{
	menus.UpdateMenuList(absX, absY);
	pad.UpdateStatus(absX, absY);
}

void __HANDLE_EVENT ULUI_LookupRange::OnMouseDown(int button, unsigned char buttonState, int absX, int absY, void *arg)
{
	int i;
	VSDL_GameEngine *game = reinterpret_cast<VSDL_GameEngine *>(arg);

	selectedMenu = -1;

	for (i = 0; i <= EXIT; i++){
		ULUI_Menu &menu = menus.GetMenu(i);

		if (menu.IsXYInRect(absX, absY)){
			selectedMenu = i;
			break;
		}
	}

	pad.OnMouseDown(absX, absY);

	//Special case for WHEEL
	if (button == SDL_BUTTON_WHEELUP){
		if (serialOffset > 0){
			serialOffset--;
		}
	}

	if (button == SDL_BUTTON_WHEELDOWN){
		serialOffset++;

		if (serialOffset + SERIALS_PER_SCREEN >= nSerials){
			serialOffset = nSerials - SERIALS_PER_SCREEN;
		}
	}
}

void __HANDLE_EVENT ULUI_LookupRange::OnQuit(void *arg)
{
	VSDL_GameEngine *game = reinterpret_cast<VSDL_GameEngine *>(arg);

	game->Quit();
}
